package com.htl.exampro.base.model;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.List;

@Data
@NoArgsConstructor
public class PageResult<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    // 数据列表
    private List<T> data;

    //总记录数
    private long total;

    //当前页码
    private long pageNumber;

    //每页记录数
    private long pageSize;

    public PageResult(List<T> data, long total, long pageNumber, long pageSize) {
        this.data = data;
        this.total = total;
        this.pageNumber = pageNumber;
        this.pageSize = pageSize;
    }

    public PageResult(long total, long pageNumber, long pageSize) {

        this.total = total;
        this.pageNumber = pageNumber;
        this.pageSize = pageSize;
    }

    public static <T> PageResult<T> result(Page<?> page, Class<T> targetType) {
        PageResult<T> result = new PageResult<>(page.getTotal(), page.getCurrent(), page.getSize());
        result.setData(BeanUtil.copyToList(page.getRecords(), targetType));
        return result;
    }
}
